import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';

import '../../../bean/bean_video_course.dart';
import '../../../utils/log_util.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/8.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
class VideoProgressRequestModel {
  var _courseToken;

  ///获取当前播放视频的状态
  Future<UserStudyCourseBean> getCurrentVideo(
      {@required int videoId, int trainId}) async {
    if (_courseToken != null && !_courseToken.isCancelled) {
      _courseToken.cancel();
    }
    _courseToken = new CancelToken();
    Map<String, dynamic> queryParameters = {
      "videoId": videoId,
    };
    if (trainId != null) {
      queryParameters["trainId"] = trainId;
    }
    //详情数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.videoStudyProgressInfoUrl,
        cancelTag: _courseToken,
        encrypt: true,
        jsonMap: queryParameters);
    if (statisRresponseInfo.success) {
      return UserStudyCourseBean.fromMap(statisRresponseInfo.data);
    } else {
      return null;
    }
  }

  /// 添加看视频记录
  requestPlayRecoder({
    int videoId,
    int configId,
  }) async {
    ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.integralStatisticsVideoAdd,
        encrypt: true,
        jsonMap: {"configId": configId, "videoId": videoId});
    if (partResponseInfo.success) {
      LogUtils.e("积分添加成功");
    } else {
      LogUtils.e("积分添加失败");
    }
  }

  requestTaskRecoder(
      {@required int taskId,
      @required String title,
      @required int itemId}) async {
    Map<String, dynamic> jsonMap = Map();
    jsonMap["taskId"] = taskId;
    jsonMap["itemId"] = itemId;
    // 0 阅读  ； 1视频 ；2 答题
    jsonMap["itemType"] = 1;
    jsonMap["itemName"] = title;

    ///添加积分记录
    ResponseInfo addTaskRecoderResonInfo = await DioUtils.instance.postRequest(
      url: HttpHelper.taskDetailsAdd,
      jsonMap: jsonMap,
    );
    if (addTaskRecoderResonInfo.success) {
      LogUtils.e("添加任务记录成功");
    } else {
      LogUtils.e("添加任务记录失败");
    }
  }

  //是否请求完成
  bool _isFinish = false;

  ///[trainId] 培训ID
  Future<bool> updateVideo(
      {int videoId,
      int current,
      int total,
      int videoCourseId,
      bool finish = false,
      int trainId}) async {
    if (_isFinish) {
      return true;
    }
    _isFinish = true;
    if (_courseToken != null && !_courseToken.isCancelled) {
      _courseToken.cancel();
    }
    _courseToken = new CancelToken();
    Map<String, dynamic> jsonMap = {
      "videoId": videoId,
      "trainId": trainId,
      "finish": finish,
      "studySecond": current,
      "videoSecond": total,
      "videoCourseId": videoCourseId,
    };
    LogUtils.e("视频进度更新 studySecond $current videoSecond $total");
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.videoStudyProgressUpdateUrl,
        cancelTag: _courseToken,
        encrypt: true,
        jsonMap: jsonMap);
    _isFinish = false;
    if (statisRresponseInfo.success) {
      return true;
    } else {}

    return false;
  }
}
